Conversation
DoTheBestToGetTheBest
commented
Jan 8, 2024
- this PR refactors the to_safe_integer and to_u32 functions into a generic to_integer function
webmaster128
left a comment
There was a problem hiding this comment.
Why this change? Looks more complicated than before. Introducing TryFrom is not needed in the original code
hello, yes i used tryfrom for fallible conversions when using tryfrom it attempts to convert one type into another, but unlike a direct cast, it can handle cases where the conversion might not be possible if you are not okey with TryFrom i could remove it :) thanks |
|
I'm primarily wondering what the advantage of the change is |
hey, so basically, it handles errors that can occur during type conversion ref : https://doc.rust-lang.org/std/convert/trait.TryFrom.html thanks:) |
|
Right, but we already checked
So the cast to u32 is always safe. What other errors can happen here that |
for example when converting a f64 to a smaller integer type , there's a risk of overflow. TryFrom safely handles these cases by returning an Err |